1 |
|
2 |
|
3 |
|
4 | const fs = require( 'fs' );
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | const isBlockMetadataExperimental = require( './src/utils/is-block-metadata-experimental' );
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 | function createBabelPlugin( shouldProcessImport, isGutenbergPlugin ) {
|
29 | if ( ! shouldProcessImport ) {
|
30 | shouldProcessImport = isImportDeclarationAnExperimentalBlock;
|
31 | }
|
32 | if ( isGutenbergPlugin === undefined ) {
|
33 |
|
34 | isGutenbergPlugin =
|
35 | String( process.env.npm_package_config_IS_GUTENBERG_PLUGIN ) ===
|
36 | 'true';
|
37 | }
|
38 | |
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | return function babelPlugin( { types: t } ) {
|
46 | if ( isGutenbergPlugin ) {
|
47 | return {};
|
48 | }
|
49 |
|
50 | return {
|
51 | visitor: {
|
52 | ImportDeclaration( path ) {
|
53 |
|
54 | if ( ! shouldProcessImport( path ) ) {
|
55 | return;
|
56 | }
|
57 |
|
58 |
|
59 | const namespaceSpecifier = path.node.specifiers.find(
|
60 | ( specifier ) =>
|
61 | specifier.type === 'ImportNamespaceSpecifier'
|
62 | );
|
63 | const { name } = namespaceSpecifier.local;
|
64 |
|
65 | path.replaceWith(
|
66 | t.variableDeclaration( 'const', [
|
67 | t.variableDeclarator(
|
68 | t.identifier( name ),
|
69 | t.nullLiteral()
|
70 | ),
|
71 | ] )
|
72 | );
|
73 | },
|
74 | },
|
75 | };
|
76 | };
|
77 | }
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 | function isImportDeclarationAnExperimentalBlock( path ) {
|
88 |
|
89 | const { node } = path;
|
90 | const namespaceSpecifier = node.specifiers.find(
|
91 | ( specifier ) => specifier.type === 'ImportNamespaceSpecifier'
|
92 | );
|
93 | if ( ! namespaceSpecifier || ! namespaceSpecifier.local ) {
|
94 | return;
|
95 | }
|
96 |
|
97 |
|
98 | const importedPath = node.source.value;
|
99 | if (
|
100 | ! importedPath ||
|
101 | ! importedPath.startsWith( './' ) ||
|
102 | importedPath.split( '/' ).length > 2
|
103 | ) {
|
104 | return false;
|
105 | }
|
106 |
|
107 |
|
108 | const blockJsonPath = __dirname + '/src/' + importedPath + '/block.json';
|
109 | if ( ! fs.existsSync( blockJsonPath ) ) {
|
110 | return false;
|
111 | }
|
112 |
|
113 |
|
114 | const { name } = namespaceSpecifier.local;
|
115 | let blockJSONBuffer;
|
116 | try {
|
117 | blockJSONBuffer = fs.readFileSync( blockJsonPath );
|
118 | } catch ( e ) {
|
119 | process.stderr.write(
|
120 | 'Could not read block.json for the module "' +
|
121 | importedPath +
|
122 | '" imported under name "' +
|
123 | name +
|
124 | '" from path "' +
|
125 | blockJsonPath +
|
126 | '"'
|
127 | );
|
128 | throw e;
|
129 | }
|
130 | let blockJSON;
|
131 | try {
|
132 | blockJSON = JSON.parse( blockJSONBuffer );
|
133 | } catch ( e ) {
|
134 | process.stderr.write(
|
135 | 'Could not parse block.json for the module "' +
|
136 | importedPath +
|
137 | '" imported under name "' +
|
138 | name +
|
139 | '" read from path "' +
|
140 | blockJsonPath +
|
141 | '"'
|
142 | );
|
143 | throw e;
|
144 | }
|
145 | if ( ! isBlockMetadataExperimental( blockJSON ) ) {
|
146 | return false;
|
147 | }
|
148 |
|
149 | return true;
|
150 | }
|
151 |
|
152 | const babelPlugin = createBabelPlugin();
|
153 | babelPlugin.createBabelPlugin = createBabelPlugin;
|
154 | module.exports = babelPlugin;
|