Skip to content

Commit 1392b0c

Browse files
committed
fix(dist-module): apply plugins to dist-module imports
1 parent db0dadb commit 1392b0c

File tree

8 files changed

+138
-32
lines changed

8 files changed

+138
-32
lines changed

src/compiler/app-core/bundle-app-core.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export const bundleApp = async (config: d.Config, compilerCtx: d.CompilerCtx, bu
5454
}),
5555
config.sys.rollup.plugins.json(),
5656
imagePlugin(config, buildCtx),
57-
cssTransformer(buildCtx),
57+
cssTransformer(config, compilerCtx, buildCtx),
5858
inMemoryFsRead(config, compilerCtx),
5959
config.sys.rollup.plugins.replace({
6060
'process.env.NODE_ENV': config.devMode ? '"development"' : '"production"'

src/compiler/browser/create-compiler.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,22 @@ export const createCompiler = () => {
1414
diagnostics.length = 0;
1515
};
1616

17+
const getResolvedData = (id: string) => {
18+
return stencilResolved.get(id);
19+
};
20+
21+
const setResolvedData = (id: string, r: d.ResolvedStencilData) => {
22+
return stencilResolved.set(id, r);
23+
};
24+
1725
return {
1826

1927
resolveId(importee: string, importer: string) {
2028
// import Css from 'stencil?tag=cmp-a&scopeId=sc-cmp-a-md&mode=md!./filepath.css
2129
const r = parseStencilImportPath(importee, importer);
2230
if (r != null) {
23-
stencilResolved.set(r.resolvedId, r);
24-
return r.resolvedId;
31+
setResolvedData(r.resolvedId, r);
32+
return r;
2533
}
2634
return null;
2735
},
@@ -34,10 +42,11 @@ export const createCompiler = () => {
3442
},
3543

3644
async transform(code: string, filePath: string, opts?: d.CompileOptions) {
37-
const r = stencilResolved.get(filePath);
45+
const r = getResolvedData(filePath);
3846
if (r != null) {
3947
const compileOpts = Object.assign({}, defaultOpts, opts);
40-
compileOpts.file = r.filePath;
48+
compileOpts.type = r.type;
49+
compileOpts.file = r.resolvedFilePath;
4150
compileOpts.data = r.data;
4251

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

58-
reset
67+
reset,
68+
getResolvedData,
69+
setResolvedData,
5970
};
6071
};

src/compiler/plugin/plugin.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,59 @@ export async function runPluginTransforms(config: d.Config, compilerCtx: d.Compi
153153

154154
return transformResults;
155155
}
156+
157+
158+
export const runPluginTransformsEsmImports = async (config: d.Config, compilerCtx: d.CompilerCtx, buildCtx: d.BuildCtx, sourceText: string, id: string) => {
159+
const pluginCtx: PluginCtx = {
160+
config: config,
161+
sys: config.sys,
162+
fs: compilerCtx.fs,
163+
cache: compilerCtx.cache,
164+
diagnostics: []
165+
};
166+
167+
const transformResults: PluginTransformResults = {
168+
code: sourceText,
169+
id: id
170+
};
171+
172+
for (const plugin of pluginCtx.config.plugins) {
173+
174+
if (typeof plugin.transform === 'function') {
175+
try {
176+
let pluginTransformResults: PluginTransformResults | string;
177+
const results = plugin.transform(transformResults.code, transformResults.id, pluginCtx);
178+
179+
if (results != null) {
180+
if (typeof (results as any).then === 'function') {
181+
pluginTransformResults = await results;
182+
183+
} else {
184+
pluginTransformResults = results as PluginTransformResults;
185+
}
186+
187+
if (pluginTransformResults != null) {
188+
if (typeof pluginTransformResults === 'string') {
189+
transformResults.code = pluginTransformResults as string;
190+
191+
} else {
192+
if (typeof pluginTransformResults.code === 'string') {
193+
transformResults.code = pluginTransformResults.code;
194+
}
195+
if (typeof pluginTransformResults.id === 'string') {
196+
transformResults.id = pluginTransformResults.id;
197+
}
198+
}
199+
}
200+
}
201+
202+
} catch (e) {
203+
catchError(buildCtx.diagnostics, e);
204+
}
205+
}
206+
}
207+
208+
buildCtx.diagnostics.push(...pluginCtx.diagnostics);
209+
210+
return transformResults;
211+
};
Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,44 @@
11
import * as d from '../../declarations';
22
import { createCompiler } from '../browser/create-compiler';
33
import { Plugin } from 'rollup';
4+
import { runPluginTransformsEsmImports } from '../plugin/plugin';
45

56

6-
export const cssTransformer = (buildCtx: d.BuildCtx): Plugin => {
7+
export const cssTransformer = (config: d.Config, compilerCtx: d.CompilerCtx, buildCtx: d.BuildCtx): Plugin => {
78
const compiler = createCompiler();
89

910
return {
1011
name: 'cssTransformer',
12+
1113
resolveId(importee: string, importer: string) {
12-
return compiler.resolveId(importee, importer);
14+
const r = compiler.resolveId(importee, importer);
15+
if (r != null && r.type === 'css') {
16+
if (KNOWN_PREPROCESSOR_EXTS.has(r.importerExt) && r.importerExt !== r.resolvedFileExt) {
17+
// basically for sass paths without an extension
18+
r.resolvedFileExt = r.importerExt;
19+
r.resolvedFileName += '.' + r.resolvedFileExt;
20+
r.resolvedFilePath += '.' + r.resolvedFileExt;
21+
r.resolvedId = `${r.resolvedFilePath}?${r.params}`;
22+
compiler.setResolvedData(r.resolvedId, r);
23+
}
24+
return r.resolvedId;
25+
}
26+
return null;
1327
},
1428

1529
async transform(code: string, id: string) {
16-
const results = await compiler.transform(code, id);
17-
if (results != null) {
18-
buildCtx.diagnostics.push(...results.diagnostics);
19-
return results;
30+
const r = compiler.getResolvedData(id);
31+
if (r != null) {
32+
const pluginTransforms = await runPluginTransformsEsmImports(config, compilerCtx, buildCtx, code, id);
33+
const results = await compiler.transform(pluginTransforms.code, id);
34+
if (results != null) {
35+
buildCtx.diagnostics.push(...results.diagnostics);
36+
return results;
37+
}
2038
}
2139
return null;
2240
}
2341
};
2442
};
43+
44+
const KNOWN_PREPROCESSOR_EXTS = new Set(['sass', 'scss', 'styl', 'less', 'pcss']);

src/compiler/rollup-plugins/stencil-public-plugin.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ export const stencilRollupPlugin = (): Plugin => {
1010
name: 'stencilPlugin',
1111

1212
resolveId(importee: string, importer: string) {
13-
return compiler.resolveId(importee, importer);
13+
const r = compiler.resolveId(importee, importer);
14+
if (r != null) {
15+
return r.resolvedId;
16+
}
17+
return null;
1418
},
1519

1620
async transform(code: string, id: string, opts: d.CompileOptions = {}) {

src/compiler/transformers/stencil-import-path.ts

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as d from '../../declarations';
2-
import { DEFAULT_STYLE_MODE, normalizePath } from '@utils';
3-
import { getScopeId } from '../style/scope-css';
2+
import { DEFAULT_STYLE_MODE, getFileExt, normalizePath } from '@utils';
43
import path from 'path';
54

65

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

3937
const dataParts = importData.split('?');
4038
if (dataParts.length === 2) {
41-
const paramsStr = dataParts[1];
42-
const params = new URLSearchParams(paramsStr);
43-
const type = params.get('type') as any;
39+
const params = dataParts[1];
40+
const urlParams = new URLSearchParams(params);
41+
const type = urlParams.get('type') as any;
4442
const data: d.StencilComponentData = {
45-
tag: params.get('tag'),
46-
scopeId: params.get('scopeId'),
47-
encapsulation: params.get('encapsulation') || 'none',
48-
mode: params.get('mode') || DEFAULT_STYLE_MODE,
43+
tag: urlParams.get('tag'),
44+
encapsulation: urlParams.get('encapsulation') || 'none',
45+
mode: urlParams.get('mode') || DEFAULT_STYLE_MODE,
4946
};
5047

5148
importer = normalizePath(importer);
5249
const importerDir = path.dirname(importer);
53-
const filePath = normalizePath(path.resolve(importerDir, importPath));
54-
const fileName = path.basename(filePath);
50+
const importerExt = getFileExt(importer.split('?')[0]);
5551

56-
let resolvedId = filePath;
52+
const resolvedFilePath = normalizePath(path.resolve(importerDir, importPath));
53+
const resolvedFileName = path.basename(resolvedFilePath);
54+
const resolvedFileExt = getFileExt(resolvedFileName);
55+
56+
let resolvedId = resolvedFilePath;
5757
if (data.encapsulation === 'scoped' && data.mode && data.mode !== DEFAULT_STYLE_MODE) {
58-
resolvedId += `?${paramsStr}`;
58+
resolvedId += `?${params}`;
5959
}
6060

6161
const r: d.ResolvedStencilData = {
6262
type,
6363
resolvedId,
64-
filePath,
65-
fileName,
64+
resolvedFilePath,
65+
resolvedFileName,
66+
resolvedFileExt,
67+
params,
6668
data,
6769
importee,
6870
importer,
71+
importerExt,
6972
};
7073

7174
return r;

src/declarations/browser-compile.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,18 @@ export type StencilDataType = 'css' | 'js' | 'ts' | 'tsx' | 'jsx' | 'dts';
3636
export interface ResolvedStencilData {
3737
type: StencilDataType;
3838
resolvedId: string;
39-
filePath: string;
40-
fileName: string;
39+
resolvedFilePath: string;
40+
resolvedFileName: string;
41+
resolvedFileExt: string;
42+
params: string;
4143
data: StencilComponentData;
4244
importee: string;
4345
importer: string;
46+
importerExt: string;
4447
}
4548

4649
export interface StencilComponentData {
4750
tag: string;
48-
scopeId: string;
4951
encapsulation?: string;
5052
mode?: string;
5153
}

src/utils/util.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,16 @@ import { BANNER } from './constants';
33
import { buildError } from './message-utils';
44

55

6+
export const getFileExt = (fileName: string) => {
7+
if (typeof fileName === 'string') {
8+
const parts = fileName.split('.');
9+
if (parts.length > 1) {
10+
return parts[parts.length - 1].toLowerCase();
11+
}
12+
}
13+
return null;
14+
};
15+
616
/**
717
* Test if a file is a typescript source file, such as .ts or .tsx.
818
* However, d.ts files and spec.ts files return false.

0 commit comments

Comments
 (0)