@compiled/webpack-loader 0.11.8 → 0.12.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/__fixtures__/custom-resolver.d.ts +2 -0
- package/dist/__fixtures__/custom-resolver.js +12 -0
- package/dist/__fixtures__/custom-resolver.js.map +1 -0
- package/dist/compiled-loader.js +17 -25
- package/dist/compiled-loader.js.map +1 -1
- package/dist/create-default-resolver.d.ts +8 -0
- package/dist/create-default-resolver.js +27 -0
- package/dist/create-default-resolver.js.map +1 -0
- package/dist/types.d.ts +4 -0
- package/package.json +4 -4
- package/src/__fixtures__/custom-resolver.tsx +8 -0
- package/src/__tests__/compiled-loader.test.ts +9 -1
- package/src/__tests__/test-utils.ts +3 -0
- package/src/compiled-loader.ts +13 -21
- package/src/create-default-resolver.ts +32 -0
- package/src/types.ts +5 -0
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.App = void 0;
|
|
4
|
+
/** @jsx jsx */
|
|
5
|
+
// @ts-ignore This is a bug where the meta is not updated for the new file import
|
|
6
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
7
|
+
const react_1 = require("@compiled/react");
|
|
8
|
+
// @ts-expect-error
|
|
9
|
+
const test_1 = require("test");
|
|
10
|
+
const App = () => (0, react_1.jsx)("div", { css: (0, react_1.css)({ color: test_1.primary }) });
|
|
11
|
+
exports.App = App;
|
|
12
|
+
//# sourceMappingURL=custom-resolver.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"custom-resolver.js","sourceRoot":"","sources":["../../src/__fixtures__/custom-resolver.tsx"],"names":[],"mappings":";;;AAAA,eAAe;AACf,iFAAiF;AACjF,6DAA6D;AAC7D,2CAA2C;AAC3C,mBAAmB;AACnB,+BAA+B;AAExB,MAAM,GAAG,GAAG,GAAgB,EAAE,CAAC,0BAAK,GAAG,EAAE,IAAA,WAAG,EAAC,EAAE,KAAK,EAAE,cAAO,EAAE,CAAC,GAAI,CAAC;AAA/D,QAAA,GAAG,OAA4D"}
|
package/dist/compiled-loader.js
CHANGED
|
@@ -10,17 +10,13 @@ var __rest = (this && this.__rest) || function (s, e) {
|
|
|
10
10
|
}
|
|
11
11
|
return t;
|
|
12
12
|
};
|
|
13
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
|
-
};
|
|
16
13
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
14
|
exports.pitch = void 0;
|
|
18
|
-
const fs_1 = __importDefault(require("fs"));
|
|
19
15
|
const path_1 = require("path");
|
|
20
16
|
const core_1 = require("@babel/core");
|
|
21
17
|
const utils_1 = require("@compiled/utils");
|
|
22
|
-
const enhanced_resolve_1 = require("enhanced-resolve");
|
|
23
18
|
const loader_utils_1 = require("loader-utils");
|
|
19
|
+
const create_default_resolver_1 = require("./create-default-resolver");
|
|
24
20
|
const extract_plugin_1 = require("./extract-plugin");
|
|
25
21
|
let hasErrored = false;
|
|
26
22
|
/**
|
|
@@ -30,7 +26,7 @@ let hasErrored = false;
|
|
|
30
26
|
* @returns
|
|
31
27
|
*/
|
|
32
28
|
function getLoaderOptions(context) {
|
|
33
|
-
const { bake = true, extract = false, importReact = undefined, nonce = undefined, resolve = {}, extensions = undefined, parserBabelPlugins = [], transformerBabelPlugins = [], [extract_plugin_1.pluginName]: isPluginEnabled = false, ssr = false, optimizeCss = true, addComponentName = false, classNameCompressionMap = undefined, extractStylesToDirectory = undefined, } = typeof context.getOptions === 'undefined'
|
|
29
|
+
const { bake = true, extract = false, importReact = undefined, nonce = undefined, resolve = {}, extensions = undefined, parserBabelPlugins = [], transformerBabelPlugins = [], [extract_plugin_1.pluginName]: isPluginEnabled = false, ssr = false, optimizeCss = true, addComponentName = false, classNameCompressionMap = undefined, extractStylesToDirectory = undefined, resolver = undefined, } = typeof context.getOptions === 'undefined'
|
|
34
30
|
? // Webpack v4 flow
|
|
35
31
|
(0, loader_utils_1.getOptions)(context)
|
|
36
32
|
: // Webpack v5 flow
|
|
@@ -79,6 +75,9 @@ function getLoaderOptions(context) {
|
|
|
79
75
|
extractStylesToDirectory: {
|
|
80
76
|
type: 'object',
|
|
81
77
|
},
|
|
78
|
+
resolver: {
|
|
79
|
+
type: 'string',
|
|
80
|
+
},
|
|
82
81
|
},
|
|
83
82
|
});
|
|
84
83
|
return {
|
|
@@ -96,6 +95,7 @@ function getLoaderOptions(context) {
|
|
|
96
95
|
addComponentName,
|
|
97
96
|
classNameCompressionMap,
|
|
98
97
|
extractStylesToDirectory,
|
|
98
|
+
resolver,
|
|
99
99
|
};
|
|
100
100
|
}
|
|
101
101
|
/**
|
|
@@ -105,7 +105,7 @@ function getLoaderOptions(context) {
|
|
|
105
105
|
* @param code
|
|
106
106
|
*/
|
|
107
107
|
async function compiledLoader(code) {
|
|
108
|
-
var _a, _b, _c, _d, _e, _f
|
|
108
|
+
var _a, _b, _c, _d, _e, _f;
|
|
109
109
|
const callback = this.async();
|
|
110
110
|
// Bail early if Compiled isn't in the module or we're looking at compiled runtime code
|
|
111
111
|
if (code.indexOf('@compiled/react') === -1 ||
|
|
@@ -114,7 +114,7 @@ async function compiledLoader(code) {
|
|
|
114
114
|
}
|
|
115
115
|
try {
|
|
116
116
|
const includedFiles = [];
|
|
117
|
-
const
|
|
117
|
+
const _g = getLoaderOptions(this), { resolve } = _g, options = __rest(_g, ["resolve"]);
|
|
118
118
|
// Transform to an AST using the local babel config.
|
|
119
119
|
const ast = await (0, core_1.parseAsync)(code, {
|
|
120
120
|
filename: this.resourcePath,
|
|
@@ -125,14 +125,6 @@ async function compiledLoader(code) {
|
|
|
125
125
|
},
|
|
126
126
|
plugins: (_b = options.transformerBabelPlugins) !== null && _b !== void 0 ? _b : undefined,
|
|
127
127
|
});
|
|
128
|
-
// Setup the default resolver, where webpack will merge any passed in options with the default
|
|
129
|
-
// resolve configuration. Ideally, we use this.getResolve({ ...resolve, useSyncFileSystemCalls: true, })
|
|
130
|
-
// However, it does not work correctly when in development mode :/
|
|
131
|
-
const resolver = enhanced_resolve_1.ResolverFactory.createResolver(Object.assign(Object.assign(Object.assign({
|
|
132
|
-
// @ts-expect-error
|
|
133
|
-
fileSystem: new enhanced_resolve_1.CachedInputFileSystem(fs_1.default, 4000) }, ((_d = (_c = this._compilation) === null || _c === void 0 ? void 0 : _c.options.resolve) !== null && _d !== void 0 ? _d : {})), resolve), {
|
|
134
|
-
// This makes the resolver invoke the callback synchronously
|
|
135
|
-
useSyncFileSystemCalls: true }));
|
|
136
128
|
// Transform using the Compiled Babel Plugin - we deliberately turn off using the local config.
|
|
137
129
|
const result = await (0, core_1.transformFromAstAsync)(ast, code, {
|
|
138
130
|
babelrc: false,
|
|
@@ -140,10 +132,10 @@ async function compiledLoader(code) {
|
|
|
140
132
|
sourceMaps: true,
|
|
141
133
|
filename: this.resourcePath,
|
|
142
134
|
parserOpts: {
|
|
143
|
-
plugins: (
|
|
135
|
+
plugins: (_c = options.parserBabelPlugins) !== null && _c !== void 0 ? _c : undefined,
|
|
144
136
|
},
|
|
145
137
|
plugins: [
|
|
146
|
-
...((
|
|
138
|
+
...((_d = options.transformerBabelPlugins) !== null && _d !== void 0 ? _d : []),
|
|
147
139
|
options.extract && [
|
|
148
140
|
'@compiled/babel-plugin-strip-runtime',
|
|
149
141
|
{
|
|
@@ -156,19 +148,19 @@ async function compiledLoader(code) {
|
|
|
156
148
|
'@compiled/babel-plugin',
|
|
157
149
|
Object.assign(Object.assign({}, options), {
|
|
158
150
|
// Turn off compressing class names if stylesheet extraction is off
|
|
159
|
-
classNameCompressionMap: options.extract && options.classNameCompressionMap, onIncludedFiles: (files) => includedFiles.push(...files), resolver:
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
151
|
+
classNameCompressionMap: options.extract && options.classNameCompressionMap, onIncludedFiles: (files) => includedFiles.push(...files), resolver: options.resolver
|
|
152
|
+
? options.resolver
|
|
153
|
+
: (0, create_default_resolver_1.createDefaultResolver)({
|
|
154
|
+
resolveOptions: resolve,
|
|
155
|
+
webpackResolveOptions: (_e = this._compilation) === null || _e === void 0 ? void 0 : _e.options.resolve,
|
|
156
|
+
}) }),
|
|
165
157
|
],
|
|
166
158
|
].filter(utils_1.toBoolean),
|
|
167
159
|
});
|
|
168
160
|
includedFiles.forEach((file) => {
|
|
169
161
|
this.addDependency((0, path_1.normalize)(file));
|
|
170
162
|
});
|
|
171
|
-
callback(null, (result === null || result === void 0 ? void 0 : result.code) || '', (
|
|
163
|
+
callback(null, (result === null || result === void 0 ? void 0 : result.code) || '', (_f = result === null || result === void 0 ? void 0 : result.map) !== null && _f !== void 0 ? _f : undefined);
|
|
172
164
|
}
|
|
173
165
|
catch (e) {
|
|
174
166
|
// @ts-expect-error Not checking for error type
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"compiled-loader.js","sourceRoot":"","sources":["../src/compiled-loader.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"compiled-loader.js","sourceRoot":"","sources":["../src/compiled-loader.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,+BAAiC;AAEjC,sCAAgE;AAChE,2CAAyD;AACzD,+CAA0C;AAG1C,uEAAkE;AAClE,qDAA8D;AAG9D,IAAI,UAAU,GAAG,KAAK,CAAC;AAEvB;;;;;GAKG;AACH,SAAS,gBAAgB,CAAC,OAA6C;IACrE,MAAM,EACJ,IAAI,GAAG,IAAI,EACX,OAAO,GAAG,KAAK,EACf,WAAW,GAAG,SAAS,EACvB,KAAK,GAAG,SAAS,EACjB,OAAO,GAAG,EAAE,EACZ,UAAU,GAAG,SAAS,EACtB,kBAAkB,GAAG,EAAE,EACvB,uBAAuB,GAAG,EAAE,EAC5B,CAAC,2BAAU,CAAC,EAAE,eAAe,GAAG,KAAK,EACrC,GAAG,GAAG,KAAK,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,iBAAiB,EAAE,qBAAqB,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC;QAC7E,QAAQ,CAAC,KAAK,CAAC,CAAC;KACjB;AACH,CAAC;AA5ED,iCA4EC;AAED,SAAgB,KAAK;IACnB,MAAM,OAAO,GAAG,gBAAgB,CAAC,IAAI,CAAC,CAAC;IACvC,IAAI,CAAC,UAAU,IAAI,OAAO,CAAC,OAAO,IAAI,CAAC,OAAO,CAAC,2BAAU,CAAC,EAAE;QAC1D,IAAI,CAAC,SAAS,CACZ,IAAA,mBAAW,EAAC,gBAAgB,CAAC,CAC3B,gLAAgL,CACjL,CACF,CAAC;QAEF,4FAA4F;QAC5F,UAAU,GAAG,IAAI,CAAC;KACnB;AACH,CAAC;AAZD,sBAYC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { Resolver } from '@compiled/babel-plugin';
|
|
2
|
+
import type { ResolveOptions } from 'webpack';
|
|
3
|
+
type Config = {
|
|
4
|
+
resolveOptions: ResolveOptions;
|
|
5
|
+
webpackResolveOptions: ResolveOptions | undefined;
|
|
6
|
+
};
|
|
7
|
+
export declare function createDefaultResolver({ resolveOptions, webpackResolveOptions }: Config): Resolver;
|
|
8
|
+
export {};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createDefaultResolver = void 0;
|
|
7
|
+
const fs_1 = __importDefault(require("fs"));
|
|
8
|
+
const path_1 = require("path");
|
|
9
|
+
const enhanced_resolve_1 = require("enhanced-resolve");
|
|
10
|
+
function createDefaultResolver({ resolveOptions, webpackResolveOptions }) {
|
|
11
|
+
// Setup the default resolver, where webpack will merge any passed in options with the default
|
|
12
|
+
// resolve configuration. Ideally, we use this.getResolve({ ...resolve, useSyncFileSystemCalls: true, })
|
|
13
|
+
// However, it does not work correctly when in development mode :/
|
|
14
|
+
const resolver = enhanced_resolve_1.ResolverFactory.createResolver(Object.assign(Object.assign(Object.assign({
|
|
15
|
+
// @ts-expect-error
|
|
16
|
+
fileSystem: new enhanced_resolve_1.CachedInputFileSystem(fs_1.default, 4000) }, (webpackResolveOptions !== null && webpackResolveOptions !== void 0 ? webpackResolveOptions : {})), resolveOptions), {
|
|
17
|
+
// This makes the resolver invoke the callback synchronously
|
|
18
|
+
useSyncFileSystemCalls: true }));
|
|
19
|
+
return {
|
|
20
|
+
// The resolver needs to be synchronous, as babel plugins must be synchronous
|
|
21
|
+
resolveSync: (context, request) => {
|
|
22
|
+
return resolver.resolveSync({}, (0, path_1.dirname)(context), request);
|
|
23
|
+
},
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
exports.createDefaultResolver = createDefaultResolver;
|
|
27
|
+
//# sourceMappingURL=create-default-resolver.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"create-default-resolver.js","sourceRoot":"","sources":["../src/create-default-resolver.ts"],"names":[],"mappings":";;;;;;AAAA,4CAAoB;AACpB,+BAA+B;AAG/B,uDAA0E;AAQ1E,SAAgB,qBAAqB,CAAC,EAAE,cAAc,EAAE,qBAAqB,EAAU;IACrF,8FAA8F;IAC9F,wGAAwG;IACxG,kEAAkE;IAClE,MAAM,QAAQ,GAAG,kCAAe,CAAC,cAAc;QAC7C,mBAAmB;QACnB,UAAU,EAAE,IAAI,wCAAqB,CAAC,YAAE,EAAE,IAAI,CAAC,IAC5C,CAAC,qBAAqB,aAArB,qBAAqB,cAArB,qBAAqB,GAAI,EAAE,CAAC,GAC7B,cAAc;QACjB,4DAA4D;QAC5D,sBAAsB,EAAE,IAAI,IAC5B,CAAC;IAEH,OAAO;QACL,6EAA6E;QAC7E,WAAW,EAAE,CAAC,OAAe,EAAE,OAAe,EAAE,EAAE;YAChD,OAAO,QAAQ,CAAC,WAAW,CAAC,EAAE,EAAE,IAAA,cAAO,EAAC,OAAO,CAAC,EAAE,OAAO,CAAW,CAAC;QACvE,CAAC;KACF,CAAC;AACJ,CAAC;AAnBD,sDAmBC"}
|
package/dist/types.d.ts
CHANGED
|
@@ -78,6 +78,10 @@ export interface CompiledLoaderOptions {
|
|
|
78
78
|
source: string;
|
|
79
79
|
dest: string;
|
|
80
80
|
};
|
|
81
|
+
/**
|
|
82
|
+
* Custom resolver for babel plugin, when set overrides default resolver
|
|
83
|
+
*/
|
|
84
|
+
resolver?: string;
|
|
81
85
|
}
|
|
82
86
|
export interface CompiledExtractPluginOptions {
|
|
83
87
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@compiled/webpack-loader",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.12.1",
|
|
4
4
|
"description": "A familiar and performant compile time CSS-in-JS library for React.",
|
|
5
5
|
"homepage": "https://compiledcssinjs.com/docs/pkg-webpack-loader",
|
|
6
6
|
"bugs": "https://github.com/atlassian-labs/compiled/issues/new?assignees=&labels=bug&template=bug_report.md",
|
|
@@ -23,15 +23,15 @@
|
|
|
23
23
|
"@babel/core": "^7.21.8",
|
|
24
24
|
"@babel/parser": "^7.21.8",
|
|
25
25
|
"@compiled/babel-plugin": "^0.26.0",
|
|
26
|
-
"@compiled/babel-plugin-strip-runtime": "^0.
|
|
26
|
+
"@compiled/babel-plugin-strip-runtime": "^0.26.2",
|
|
27
27
|
"@compiled/css": "^0.12.1",
|
|
28
|
-
"@compiled/utils": "^0.9.
|
|
28
|
+
"@compiled/utils": "^0.9.1",
|
|
29
29
|
"enhanced-resolve": "^5.15.0",
|
|
30
30
|
"loader-utils": "^2.0.4",
|
|
31
31
|
"webpack-sources": "^3.2.3"
|
|
32
32
|
},
|
|
33
33
|
"devDependencies": {
|
|
34
|
-
"@compiled/react": "^0.16.
|
|
34
|
+
"@compiled/react": "^0.16.5",
|
|
35
35
|
"babel-loader": "^9.1.2",
|
|
36
36
|
"css-loader": "^6.7.3",
|
|
37
37
|
"memfs": "^3.5.3",
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** @jsx jsx */
|
|
2
|
+
// @ts-ignore This is a bug where the meta is not updated for the new file import
|
|
3
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
4
|
+
import { jsx, css } from '@compiled/react';
|
|
5
|
+
// @ts-expect-error
|
|
6
|
+
import { primary } from 'test';
|
|
7
|
+
|
|
8
|
+
export const App = (): JSX.Element => <div css={css({ color: primary })} />;
|
|
@@ -75,11 +75,19 @@ describe.each<'development' | 'production'>(['development', 'production'])(
|
|
|
75
75
|
expect(assets['main.js']).toInclude('._syaz1if8{color:indigo}');
|
|
76
76
|
});
|
|
77
77
|
|
|
78
|
+
it('transforms styles imported through an overridden resolver', async () => {
|
|
79
|
+
const assets = await bundle(join(fixturesPath, 'custom-resolver.tsx'), {
|
|
80
|
+
resolver: '@compiled-private/resolver-webpack',
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
expect(assets['main.js']).toInclude('._syaz8p1k{color:very-very red color');
|
|
84
|
+
});
|
|
85
|
+
|
|
78
86
|
it('fails when using unrecognised compiled syntax', async () => {
|
|
79
87
|
await expect(bundle(join(fixturesPath, 'compiled-error.tsx'))).rejects.toEqual([
|
|
80
88
|
expect.objectContaining({
|
|
81
89
|
message: expect.stringContaining(
|
|
82
|
-
|
|
90
|
+
'This BooleanLiteral was unable to have its styles extracted — try to define them statically using Compiled APIs instead'
|
|
83
91
|
),
|
|
84
92
|
}),
|
|
85
93
|
]);
|
|
@@ -16,6 +16,7 @@ export interface BundleOptions {
|
|
|
16
16
|
disableCacheGroup?: boolean;
|
|
17
17
|
mode: 'development' | 'production';
|
|
18
18
|
resolve?: ResolveOptions;
|
|
19
|
+
resolver?: string;
|
|
19
20
|
}
|
|
20
21
|
|
|
21
22
|
export function bundle(
|
|
@@ -27,6 +28,7 @@ export function bundle(
|
|
|
27
28
|
disableCacheGroup = false,
|
|
28
29
|
mode,
|
|
29
30
|
resolve = {},
|
|
31
|
+
resolver,
|
|
30
32
|
}: BundleOptions
|
|
31
33
|
): Promise<Record<string, string>> {
|
|
32
34
|
const outputPath = join(__dirname, 'dist');
|
|
@@ -56,6 +58,7 @@ export function bundle(
|
|
|
56
58
|
importReact: false,
|
|
57
59
|
optimizeCss: false,
|
|
58
60
|
resolve,
|
|
61
|
+
resolver,
|
|
59
62
|
},
|
|
60
63
|
},
|
|
61
64
|
],
|
package/src/compiled-loader.ts
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
import
|
|
2
|
-
import { dirname, normalize } from 'path';
|
|
1
|
+
import { normalize } from 'path';
|
|
3
2
|
|
|
4
3
|
import { parseAsync, transformFromAstAsync } from '@babel/core';
|
|
5
4
|
import { createError, toBoolean } from '@compiled/utils';
|
|
6
|
-
import { CachedInputFileSystem, ResolverFactory } from 'enhanced-resolve';
|
|
7
5
|
import { getOptions } from 'loader-utils';
|
|
8
6
|
import type { LoaderContext } from 'webpack';
|
|
9
7
|
|
|
8
|
+
import { createDefaultResolver } from './create-default-resolver';
|
|
10
9
|
import { pluginName, styleSheetName } from './extract-plugin';
|
|
11
10
|
import type { CompiledLoaderOptions } from './types';
|
|
12
11
|
|
|
@@ -34,6 +33,7 @@ function getLoaderOptions(context: LoaderContext<CompiledLoaderOptions>) {
|
|
|
34
33
|
addComponentName = false,
|
|
35
34
|
classNameCompressionMap = undefined,
|
|
36
35
|
extractStylesToDirectory = undefined,
|
|
36
|
+
resolver = undefined,
|
|
37
37
|
}: CompiledLoaderOptions = typeof context.getOptions === 'undefined'
|
|
38
38
|
? // Webpack v4 flow
|
|
39
39
|
getOptions(context)
|
|
@@ -83,6 +83,9 @@ function getLoaderOptions(context: LoaderContext<CompiledLoaderOptions>) {
|
|
|
83
83
|
extractStylesToDirectory: {
|
|
84
84
|
type: 'object',
|
|
85
85
|
},
|
|
86
|
+
resolver: {
|
|
87
|
+
type: 'string',
|
|
88
|
+
},
|
|
86
89
|
},
|
|
87
90
|
});
|
|
88
91
|
|
|
@@ -101,6 +104,7 @@ function getLoaderOptions(context: LoaderContext<CompiledLoaderOptions>) {
|
|
|
101
104
|
addComponentName,
|
|
102
105
|
classNameCompressionMap,
|
|
103
106
|
extractStylesToDirectory,
|
|
107
|
+
resolver,
|
|
104
108
|
};
|
|
105
109
|
}
|
|
106
110
|
|
|
@@ -139,18 +143,6 @@ export default async function compiledLoader(
|
|
|
139
143
|
plugins: options.transformerBabelPlugins ?? undefined,
|
|
140
144
|
});
|
|
141
145
|
|
|
142
|
-
// Setup the default resolver, where webpack will merge any passed in options with the default
|
|
143
|
-
// resolve configuration. Ideally, we use this.getResolve({ ...resolve, useSyncFileSystemCalls: true, })
|
|
144
|
-
// However, it does not work correctly when in development mode :/
|
|
145
|
-
const resolver = ResolverFactory.createResolver({
|
|
146
|
-
// @ts-expect-error
|
|
147
|
-
fileSystem: new CachedInputFileSystem(fs, 4000),
|
|
148
|
-
...(this._compilation?.options.resolve ?? {}),
|
|
149
|
-
...resolve,
|
|
150
|
-
// This makes the resolver invoke the callback synchronously
|
|
151
|
-
useSyncFileSystemCalls: true,
|
|
152
|
-
});
|
|
153
|
-
|
|
154
146
|
// Transform using the Compiled Babel Plugin - we deliberately turn off using the local config.
|
|
155
147
|
const result = await transformFromAstAsync(ast!, code, {
|
|
156
148
|
babelrc: false,
|
|
@@ -177,12 +169,12 @@ export default async function compiledLoader(
|
|
|
177
169
|
// Turn off compressing class names if stylesheet extraction is off
|
|
178
170
|
classNameCompressionMap: options.extract && options.classNameCompressionMap,
|
|
179
171
|
onIncludedFiles: (files: string[]) => includedFiles.push(...files),
|
|
180
|
-
resolver:
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
172
|
+
resolver: options.resolver
|
|
173
|
+
? options.resolver
|
|
174
|
+
: createDefaultResolver({
|
|
175
|
+
resolveOptions: resolve,
|
|
176
|
+
webpackResolveOptions: this._compilation?.options.resolve,
|
|
177
|
+
}),
|
|
186
178
|
},
|
|
187
179
|
],
|
|
188
180
|
].filter(toBoolean),
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import { dirname } from 'path';
|
|
3
|
+
|
|
4
|
+
import type { Resolver } from '@compiled/babel-plugin';
|
|
5
|
+
import { CachedInputFileSystem, ResolverFactory } from 'enhanced-resolve';
|
|
6
|
+
import type { ResolveOptions } from 'webpack';
|
|
7
|
+
|
|
8
|
+
type Config = {
|
|
9
|
+
resolveOptions: ResolveOptions;
|
|
10
|
+
webpackResolveOptions: ResolveOptions | undefined;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export function createDefaultResolver({ resolveOptions, webpackResolveOptions }: Config): Resolver {
|
|
14
|
+
// Setup the default resolver, where webpack will merge any passed in options with the default
|
|
15
|
+
// resolve configuration. Ideally, we use this.getResolve({ ...resolve, useSyncFileSystemCalls: true, })
|
|
16
|
+
// However, it does not work correctly when in development mode :/
|
|
17
|
+
const resolver = ResolverFactory.createResolver({
|
|
18
|
+
// @ts-expect-error
|
|
19
|
+
fileSystem: new CachedInputFileSystem(fs, 4000),
|
|
20
|
+
...(webpackResolveOptions ?? {}),
|
|
21
|
+
...resolveOptions,
|
|
22
|
+
// This makes the resolver invoke the callback synchronously
|
|
23
|
+
useSyncFileSystemCalls: true,
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
return {
|
|
27
|
+
// The resolver needs to be synchronous, as babel plugins must be synchronous
|
|
28
|
+
resolveSync: (context: string, request: string) => {
|
|
29
|
+
return resolver.resolveSync({}, dirname(context), request) as string;
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
}
|
package/src/types.ts
CHANGED
|
@@ -91,6 +91,11 @@ export interface CompiledLoaderOptions {
|
|
|
91
91
|
* When set, extract styles to an external CSS file
|
|
92
92
|
*/
|
|
93
93
|
extractStylesToDirectory?: { source: string; dest: string };
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* Custom resolver for babel plugin, when set overrides default resolver
|
|
97
|
+
*/
|
|
98
|
+
resolver?: string;
|
|
94
99
|
}
|
|
95
100
|
|
|
96
101
|
export interface CompiledExtractPluginOptions {
|