Skip to content

Commit

Permalink
T-345 use loadconfig for posthtml (#4752)
Browse files Browse the repository at this point in the history
* use new config functions for posthtml

* remove getconfig

* fix tests

* make . in canonical optional although I dont know how this test ever could fail

* cleanup
  • Loading branch information
DeMoorJasper authored Jun 18, 2020
1 parent b8a9161 commit 7fa1efa
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 36 deletions.
15 changes: 3 additions & 12 deletions packages/core/core/src/Transformation.js
Original file line number Diff line number Diff line change
Expand Up @@ -535,18 +535,6 @@ async function runTransformer(
).filePath;
};

// Load config for the transformer.
let config = preloadedConfig;
if (transformer.getConfig) {
// TODO: deprecate getConfig
config = await transformer.getConfig({
asset: new MutableAsset(asset),
options: pipeline.pluginOptions,
resolve,
logger,
});
}

// If an ast exists on the asset, but we cannot reuse it,
// use the previous transform to generate code that we can re-parse.
if (
Expand All @@ -565,6 +553,9 @@ async function runTransformer(
asset.mapBuffer = output.map?.toBuffer();
}

// Load config for the transformer.
let config = preloadedConfig;

// Parse if there is no AST available from a previous transform.
if (!asset.ast && transformer.parse) {
let ast = await transformer.parse({
Expand Down
2 changes: 1 addition & 1 deletion packages/core/integration-tests/test/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ describe('html', function() {
'utf8',
);

assert(/<link rel="canonical" href="\/index.html">/.test(html));
assert(/<link rel="canonical" href="\.?\/index.html">/.test(html));
});

it('should support meta tag with none content', async function() {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/integration-tests/test/pug.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('pug', function() {
name: 'index.html',
assets: ['index.pug'],
includedFiles: {
'index.pug': ['package.json', 'base.pug', 'other.pug', 'nested.pug'],
'index.pug': ['base.pug', 'other.pug', 'nested.pug'],
},
},
]);
Expand Down
5 changes: 0 additions & 5 deletions packages/core/parcel/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1390,11 +1390,6 @@ asset graph. They mostly call out to different compilers and preprocessors.
import {Transform} from '@parcel/plugin';

export default new Transform({
async getConfig({asset}) {
// ...
return config;
},

async parse({asset}) {
// ...
return ast;
Expand Down
7 changes: 0 additions & 7 deletions packages/core/types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -504,13 +504,6 @@ export type MultiThreadValidator = {|
export type Validator = DedicatedThreadValidator | MultiThreadValidator;

export type Transformer = {|
// TODO: deprecate getConfig
getConfig?: ({|
asset: MutableAsset,
resolve: ResolveFn,
options: PluginOptions,
logger: PluginLogger,
|}) => Async<ConfigResult | void>,
loadConfig?: ({|
config: Config,
options: PluginOptions,
Expand Down
47 changes: 37 additions & 10 deletions packages/transformers/posthtml/src/PostHTMLTransformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import {Transformer} from '@parcel/plugin';

import path from 'path';
import posthtml from 'posthtml';
import parse from 'posthtml-parser';
import render from 'posthtml-render';
Expand All @@ -10,22 +11,38 @@ import semver from 'semver';
import loadPlugins from './loadPlugins';

export default new Transformer({
async getConfig({asset, options}) {
let config = await asset.getConfig(
async loadConfig({config}) {
let configFile = await config.getConfig(
['.posthtmlrc', '.posthtmlrc.js', 'posthtml.config.js'],
{
packageKey: 'posthtml',
},
);

config = config || {};
if (configFile) {
let isJavascript = path.extname(configFile.filePath) === '.js';
if (isJavascript) {
config.shouldInvalidateOnStartup();
config.shouldReload();
}

// load plugins
config.plugins = await loadPlugins(config.plugins, asset.filePath, options);
// tells posthtml that we have already called parse
configFile.contents.skipParse = true;

config.setResult({
contents: configFile.contents,
isSerialisable: !isJavascript,
});
}
},

preSerializeConfig({config}) {
if (!config.result) return;

// tells posthtml that we have already called parse
config.skipParse = true;
return config;
// Ensure we dont try to serialise functions
if (!config.result.isSerialisable) {
config.result.contents = {};
}
},

canReuseAST({ast}) {
Expand All @@ -47,13 +64,23 @@ export default new Transformer({
};
},

async transform({asset, config}) {
async transform({asset, config, options}) {
if (!config) {
return [asset];
}

// load plugins
const plugins = await loadPlugins(
config.contents.plugins,
asset.filePath,
options,
);

let ast = nullthrows(await asset.getAST());
let res = await posthtml(config.plugins).process(ast.program, config);
let res = await posthtml(plugins).process(ast.program, {
...config.contents,
plugins,
});

if (res.messages) {
await Promise.all(
Expand Down

0 comments on commit 7fa1efa

Please sign in to comment.