Skip to content

Commit

Permalink
fix(dist-module): apply plugins to dist-module imports
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdbradley committed Aug 14, 2019
1 parent db0dadb commit 1392b0c
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/compiler/app-core/bundle-app-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const bundleApp = async (config: d.Config, compilerCtx: d.CompilerCtx, bu
}),
config.sys.rollup.plugins.json(),
imagePlugin(config, buildCtx),
cssTransformer(buildCtx),
cssTransformer(config, compilerCtx, buildCtx),
inMemoryFsRead(config, compilerCtx),
config.sys.rollup.plugins.replace({
'process.env.NODE_ENV': config.devMode ? '"development"' : '"production"'
Expand Down
21 changes: 16 additions & 5 deletions src/compiler/browser/create-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,22 @@ export const createCompiler = () => {
diagnostics.length = 0;
};

const getResolvedData = (id: string) => {
return stencilResolved.get(id);
};

const setResolvedData = (id: string, r: d.ResolvedStencilData) => {
return stencilResolved.set(id, r);
};

return {

resolveId(importee: string, importer: string) {
// import Css from 'stencil?tag=cmp-a&scopeId=sc-cmp-a-md&mode=md!./filepath.css
const r = parseStencilImportPath(importee, importer);
if (r != null) {
stencilResolved.set(r.resolvedId, r);
return r.resolvedId;
setResolvedData(r.resolvedId, r);
return r;
}
return null;
},
Expand All @@ -34,10 +42,11 @@ export const createCompiler = () => {
},

async transform(code: string, filePath: string, opts?: d.CompileOptions) {
const r = stencilResolved.get(filePath);
const r = getResolvedData(filePath);
if (r != null) {
const compileOpts = Object.assign({}, defaultOpts, opts);
compileOpts.file = r.filePath;
compileOpts.type = r.type;
compileOpts.file = r.resolvedFilePath;
compileOpts.data = r.data;

const results = await compile(code, compileOpts);
Expand All @@ -55,6 +64,8 @@ export const createCompiler = () => {
reset();
},

reset
reset,
getResolvedData,
setResolvedData,
};
};
56 changes: 56 additions & 0 deletions src/compiler/plugin/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,59 @@ export async function runPluginTransforms(config: d.Config, compilerCtx: d.Compi

return transformResults;
}


export const runPluginTransformsEsmImports = async (config: d.Config, compilerCtx: d.CompilerCtx, buildCtx: d.BuildCtx, sourceText: string, id: string) => {
const pluginCtx: PluginCtx = {
config: config,
sys: config.sys,
fs: compilerCtx.fs,
cache: compilerCtx.cache,
diagnostics: []
};

const transformResults: PluginTransformResults = {
code: sourceText,
id: id
};

for (const plugin of pluginCtx.config.plugins) {

if (typeof plugin.transform === 'function') {
try {
let pluginTransformResults: PluginTransformResults | string;
const results = plugin.transform(transformResults.code, transformResults.id, pluginCtx);

if (results != null) {
if (typeof (results as any).then === 'function') {
pluginTransformResults = await results;

} else {
pluginTransformResults = results as PluginTransformResults;
}

if (pluginTransformResults != null) {
if (typeof pluginTransformResults === 'string') {
transformResults.code = pluginTransformResults as string;

} else {
if (typeof pluginTransformResults.code === 'string') {
transformResults.code = pluginTransformResults.code;
}
if (typeof pluginTransformResults.id === 'string') {
transformResults.id = pluginTransformResults.id;
}
}
}
}

} catch (e) {
catchError(buildCtx.diagnostics, e);
}
}
}

buildCtx.diagnostics.push(...pluginCtx.diagnostics);

return transformResults;
};
32 changes: 26 additions & 6 deletions src/compiler/rollup-plugins/css-transformer.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,44 @@
import * as d from '../../declarations';
import { createCompiler } from '../browser/create-compiler';
import { Plugin } from 'rollup';
import { runPluginTransformsEsmImports } from '../plugin/plugin';


export const cssTransformer = (buildCtx: d.BuildCtx): Plugin => {
export const cssTransformer = (config: d.Config, compilerCtx: d.CompilerCtx, buildCtx: d.BuildCtx): Plugin => {
const compiler = createCompiler();

return {
name: 'cssTransformer',

resolveId(importee: string, importer: string) {
return compiler.resolveId(importee, importer);
const r = compiler.resolveId(importee, importer);
if (r != null && r.type === 'css') {
if (KNOWN_PREPROCESSOR_EXTS.has(r.importerExt) && r.importerExt !== r.resolvedFileExt) {
// basically for sass paths without an extension
r.resolvedFileExt = r.importerExt;
r.resolvedFileName += '.' + r.resolvedFileExt;
r.resolvedFilePath += '.' + r.resolvedFileExt;
r.resolvedId = `${r.resolvedFilePath}?${r.params}`;
compiler.setResolvedData(r.resolvedId, r);
}
return r.resolvedId;
}
return null;
},

async transform(code: string, id: string) {
const results = await compiler.transform(code, id);
if (results != null) {
buildCtx.diagnostics.push(...results.diagnostics);
return results;
const r = compiler.getResolvedData(id);
if (r != null) {
const pluginTransforms = await runPluginTransformsEsmImports(config, compilerCtx, buildCtx, code, id);
const results = await compiler.transform(pluginTransforms.code, id);
if (results != null) {
buildCtx.diagnostics.push(...results.diagnostics);
return results;
}
}
return null;
}
};
};

const KNOWN_PREPROCESSOR_EXTS = new Set(['sass', 'scss', 'styl', 'less', 'pcss']);
6 changes: 5 additions & 1 deletion src/compiler/rollup-plugins/stencil-public-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ export const stencilRollupPlugin = (): Plugin => {
name: 'stencilPlugin',

resolveId(importee: string, importer: string) {
return compiler.resolveId(importee, importer);
const r = compiler.resolveId(importee, importer);
if (r != null) {
return r.resolvedId;
}
return null;
},

async transform(code: string, id: string, opts: d.CompileOptions = {}) {
Expand Down
35 changes: 19 additions & 16 deletions src/compiler/transformers/stencil-import-path.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as d from '../../declarations';
import { DEFAULT_STYLE_MODE, normalizePath } from '@utils';
import { getScopeId } from '../style/scope-css';
import { DEFAULT_STYLE_MODE, getFileExt, normalizePath } from '@utils';
import path from 'path';


Expand All @@ -13,7 +12,6 @@ export const createStencilImportPath = (type: d.StencilDataType, tagName: string
const serializeStencilImportPath = (type: d.StencilDataType, tagName: string, encapsulation: string, modeName: string) => {
const data: d.StencilComponentData = {
tag: tagName,
scopeId: getScopeId(tagName, modeName)
};
if (modeName && modeName !== DEFAULT_STYLE_MODE) {
data.mode = modeName;
Expand All @@ -38,34 +36,39 @@ export const parseStencilImportPath = (importee: string, importer: string) => {

const dataParts = importData.split('?');
if (dataParts.length === 2) {
const paramsStr = dataParts[1];
const params = new URLSearchParams(paramsStr);
const type = params.get('type') as any;
const params = dataParts[1];
const urlParams = new URLSearchParams(params);
const type = urlParams.get('type') as any;
const data: d.StencilComponentData = {
tag: params.get('tag'),
scopeId: params.get('scopeId'),
encapsulation: params.get('encapsulation') || 'none',
mode: params.get('mode') || DEFAULT_STYLE_MODE,
tag: urlParams.get('tag'),
encapsulation: urlParams.get('encapsulation') || 'none',
mode: urlParams.get('mode') || DEFAULT_STYLE_MODE,
};

importer = normalizePath(importer);
const importerDir = path.dirname(importer);
const filePath = normalizePath(path.resolve(importerDir, importPath));
const fileName = path.basename(filePath);
const importerExt = getFileExt(importer.split('?')[0]);

let resolvedId = filePath;
const resolvedFilePath = normalizePath(path.resolve(importerDir, importPath));
const resolvedFileName = path.basename(resolvedFilePath);
const resolvedFileExt = getFileExt(resolvedFileName);

let resolvedId = resolvedFilePath;
if (data.encapsulation === 'scoped' && data.mode && data.mode !== DEFAULT_STYLE_MODE) {
resolvedId += `?${paramsStr}`;
resolvedId += `?${params}`;
}

const r: d.ResolvedStencilData = {
type,
resolvedId,
filePath,
fileName,
resolvedFilePath,
resolvedFileName,
resolvedFileExt,
params,
data,
importee,
importer,
importerExt,
};

return r;
Expand Down
8 changes: 5 additions & 3 deletions src/declarations/browser-compile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,18 @@ export type StencilDataType = 'css' | 'js' | 'ts' | 'tsx' | 'jsx' | 'dts';
export interface ResolvedStencilData {
type: StencilDataType;
resolvedId: string;
filePath: string;
fileName: string;
resolvedFilePath: string;
resolvedFileName: string;
resolvedFileExt: string;
params: string;
data: StencilComponentData;
importee: string;
importer: string;
importerExt: string;
}

export interface StencilComponentData {
tag: string;
scopeId: string;
encapsulation?: string;
mode?: string;
}
10 changes: 10 additions & 0 deletions src/utils/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ import { BANNER } from './constants';
import { buildError } from './message-utils';


export const getFileExt = (fileName: string) => {
if (typeof fileName === 'string') {
const parts = fileName.split('.');
if (parts.length > 1) {
return parts[parts.length - 1].toLowerCase();
}
}
return null;
};

/**
* Test if a file is a typescript source file, such as .ts or .tsx.
* However, d.ts files and spec.ts files return false.
Expand Down

0 comments on commit 1392b0c

Please sign in to comment.